app.controller('skuDetailCtrl',['$scope','$stateParams','$modal','$log','utilService','skuDetailService','toaster',function($scope,$stateParams,$modal,$log,utilService,skuDetailService,toaster){

    //查询sku规格
    $scope.query = function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'sku/items',
            params:{skuId:$stateParams.skuId}
        }).then(function(result){
        });
    }

    $scope.query();


    //打开添加弹窗
    $scope.openAddSku = function () {
        var modalInstance = $modal.open({
            templateUrl: 'skuItemsPopupAdd.html',
            controller: 'skuItemsAddCtrl',
            size: 'sm',
            scope:$scope,
            resolve:{
                data:function(){
                    return {skuId:$stateParams.skuId}
                }
            }
        });

        modalInstance.result.then(function (result) {
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    };


    //打开删除弹窗
    $scope.openDeleteSku = function (id,standard) {
        skuDetailService.queryGoodsAboutSkuItemsCount($scope, id).then(function(result){
            switch (result.status){
                case 200:
                    if(result.data>0){
                        alert("有商品正在使用,无法更改");
                    }else{
                        $scope.openDelete(id,standard);
                    }
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });


    };

    $scope.openDelete=function(id,standard){
        var modalInstance = $modal.open({
            templateUrl: 'skuItemsPopupDelete.html',
            controller: 'skuItemsDeleteCtrl',
            size: 'sm',
            scope:$scope,
            resolve:{
                data:function(){
                    return {id:id,standard:standard};
                }
            }
        });

        modalInstance.result.then(function (result) {
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    }

    //打开批量删除弹窗
    $scope.openBatchDeleteSku = function (id,name) {

        if($scope.selected.length<=0){
            alert("请先勾选选项");
            return;
        }

        var modalInstance = $modal.open({
            templateUrl: 'skuItemsPopupBatchDelete.html',
            controller: 'skuItemsBatchDeleteCtrl',
            size: 'sm',
            scope:$scope,
            resolve:{
                data:function(){
                    return $scope.selected;
                }
            }
        });

        modalInstance.result.then(function (result) {
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });

    };


    $scope.openEditSku = function (id,standard) {
        skuDetailService.queryGoodsAboutSkuItemsCount($scope, id).then(function(result){
            switch (result.status){
                case 200:
                    if(result.data>0){
                        alert("有商品正在使用,无法更改");
                    }else{
                        $scope.openEdit(id,standard);
                    }
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break
            }
        });
    };


    $scope.openEdit=function(id,standard){
        var modalInstance = $modal.open({
            templateUrl: 'skuItemsPopupEdit.html',
            controller: 'skuItemsEditCtrl',
            size: 'sm',
            scope:$scope,
            resolve:{
                data:function(){
                    return {id:id,standard:standard};
                }
            }
        });

        modalInstance.result.then(function (result) {
        }, function () {
            $log.info('Modal dismissed at: ' + new Date());
        });
    }

}]);


app.controller('skuItemsAddCtrl',['$scope','$modalInstance','skuDetailService','$state','toaster','data',function($scope,$modalInstance,skuDetailService,$state,toaster,data){

    $scope.skuDetailService=skuDetailService;
    skuDetailService.form.skuId=data.skuId;

    $scope.allowToAddSkuitems=true;

    $scope.skuItemsAdd=function(){

        if(!$scope.allowToAddSkuitems){
            return;
        }

        $scope.allowToAddSkuitems=false;

        skuDetailService.skuItemsadd($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.allowToAddSkuitems=true;
                    $modalInstance.close("success");
                    $scope.query();
                    toaster.pop('success', "提示", "操作成功");
                    break;
                case 618:
                case 630:
                    $scope.allowToAddSkuitems=true;
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default :
                    $scope.allowToAddSkuitems=true;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
         });
    }

    $scope.cancel=function(){
        $modalInstance.close("cancel");
    }

}]);


app.controller('skuItemsDeleteCtrl', ['$scope', '$modalInstance','$state','skuDetailService','data','toaster', function($scope, $modalInstance,$state,skuDetailService,data,toaster) {

    $scope.data=data;

    $scope.allowToDelSkuItems=true;

    $scope.deleteSkuItems=function(){

        if(!$scope.allowToDelSkuItems){
            return;
        }

        $scope.allowToDelSkuItems=false;

        skuDetailService.deleteSkuItems($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.allowToDelSkuItems=true;
                    $modalInstance.close("success");
                    $scope.query();
                    toaster.pop('success', "提示", "操作成功");
                    break;
                default :
                    $scope.allowToDelSkuItems=true;
                    $modalInstance.close("error");
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.cancel = function () {
        $modalInstance.close("关闭");
    };
}])
;

app.controller('skuItemsBatchDeleteCtrl', ['$scope', '$modalInstance','$state','skuDetailService','data','toaster', function($scope, $modalInstance,$state,skuDetailService,data,toaster) {

    $scope.data=data;
    $scope.allowToBatchDelSkuItems=true;

    $scope.batchdeleteSku=function(){

        if(!$scope.allowToBatchDelSkuItems){
            return;
        }

        $scope.allowToBatchDelSkuItems=false;

        skuDetailService.batchdeleteSkuItems($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.allowToBatchDelSkuItems=true;
                    $modalInstance.close("success");
                    $scope.query();
                    toaster.pop('success', "提示", "操作成功");
                    break;
                default :
                    $scope.allowToBatchDelSkuItems=true;
                    $modalInstance.close("error");
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.cancel = function () {
        $modalInstance.close("关闭");
    };


}])
;


app.controller('skuItemsEditCtrl', ['$scope', '$modalInstance','$state','skuDetailService','data','toaster', function($scope, $modalInstance,$state,skuDetailService,data,toaster) {
    $scope.data=data;

    $scope.updateAllowSkuItems=true;

    $scope.updateSkuItems=function(){

        if(!$scope.updateAllowSkuItems){
            return;
        }

        $scope.updateAllowSkuItems=false;

        skuDetailService.updateSkuItems($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.updateAllowSkuItems=true;
                    $modalInstance.close("success");
                    $scope.query();
                    toaster.pop('success', "提示", "操作成功");
                    break;
                case 630:
                case 631:
                    $scope.updateAllowSkuItems=true;
                    toaster.pop('waiting', "提示", result.message);
                    break;
                default:
                    $scope.updateAllowSkuItems=true;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }
    $scope.cancel = function () {
        $modalInstance.close("关闭");
    };


}])
;